package com.example.demo.dao;
import com.example.demo.pojo.Classed;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.data.repository.query.QueryByExampleExecutor;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

@Repository
@Mapper
public interface DaoClass extends JpaRepository<Classed,String>, QueryByExampleExecutor<Classed> {
    Page<Classed> findByClassNameContainingOrClassTeacherContaining(String className, String teacher, Pageable pageable);
    int countByClassNameContainsOrClassTeacherContaining(String classname,String classTeacher);
    @Query(nativeQuery=true,value = "select  class.* from class inner join class_user on class.class_id=class_user.class_id where user_id=:userId" +
            " and ( class_name like %:name% or  class_name like %:name% )")
    Page<Classed> getAllByUserId(@Param("name") String name, @Param("userId")String userId, Pageable pageable);

    @Query(nativeQuery=true,value = "select COUNT(*) from class inner join class_user on class.class_id=class_user.class_id where user_id=:userId" +
            " and ( class_name like %:name% or  class_name like %:name%)")
    Long getNumByUserId(@Param("name") String name, @Param("userId")String userId);

    @Query(value = "select class_id,class_name from class",nativeQuery = true)
    List<Map<String,String>> getAllClass();

    Classed findByClassId(String s);
}
